package com.hoshiicloud.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.goods.entity.goods.GoodsSpecifications;
import com.hoshiicloud.goods.param.goods.GoodsSpecificationsAddParam;
import com.hoshiicloud.goods.param.goods.GoodsSpecificationsQueryParam;
import com.hoshiicloud.goods.param.goods.GoodsSpecificationsUpdateParam;
import com.hoshiicloud.goods.service.GoodsSpecificationsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 商品规格 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-04
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/goods/goods-specifications")
@Api(value = "商品规格", tags = {"商品规格"})
public class GoodsSpecificationsController {
    @Autowired
    private GoodsSpecificationsService goodsSpecificationsService;

    @ApiOperation(value = "分页查询商品规格")
    @PostMapping("/getPage")
    public Response<Page<GoodsSpecifications>> page(@RequestBody GoodsSpecificationsQueryParam param) {
        try {
            QueryWrapper<GoodsSpecifications> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq( GoodsSpecifications::getCompanyId, param.getCompanyId())
                    .like(StringUtils.isNotBlank(param.getSpecName()), GoodsSpecifications::getSpecName, param.getSpecName())
                    .eq(StringUtils.isNotBlank(param.getSpecType()), GoodsSpecifications::getSpecType, param.getSpecType())
                    .eq(GoodsSpecifications::getDeleted,0);
            Page<GoodsSpecifications> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = "GoodsSpecificationsQueryParam{"+param.toString() + "},Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(goodsSpecificationsService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询商品规格异常", e);
            return Response.failed("分页查询商品规格异常");
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询商品规格")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(goodsSpecificationsService.getById(id));
    }


    @PostMapping("/save")
    @ApiOperation(value = "新增商品规格")
    public Response<Boolean> save(@RequestBody GoodsSpecificationsAddParam param) {
        // Bean数据转换
        GoodsSpecifications entity;
        try {
            entity = BeanUtils.covertType(param, GoodsSpecifications.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(goodsSpecificationsService.save(entity));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改商品规格")
    public Response<Boolean> update(@RequestBody GoodsSpecificationsUpdateParam param) {
        // Bean数据转换
        GoodsSpecifications entity;
        try {
            entity = BeanUtils.covertType(param, GoodsSpecifications.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(goodsSpecificationsService.updateById(entity));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除商品规格")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response<Boolean> delete(@PathVariable("id") Long id) {
        return Response.data(goodsSpecificationsService.deleteById(id));
    }

    @ApiOperation(value = "根据id集合查询商品规格")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.failed("id不能为空");
        }
        ids.sort(null);
        return Response.data(goodsSpecificationsService.getByIds(ids, StringUtils.join(ids, "")));
    }
}
